package com.sk.waste.app.utils

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat


/**
 * @Created by Im_Jingwei
 * @time 2024/4/11  13:39
 * @Describe 权限申请功能类
 */
class PermissionUtil {
    private var mActivity: Activity? = null
    private val missingPermission: MutableList<String> = ArrayList()
    private val REQUEST_PERMISSION_CODE = 12345


    private val REQUIRED_PERMISSION_LIST = arrayOf(
        Manifest.permission.INTERNET,
        Manifest.permission.CAMERA,
//        Manifest.permission.RECORD_AUDIO,
        Manifest.permission.ACCESS_NETWORK_STATE,
        Manifest.permission.ACCESS_WIFI_STATE,
        Manifest.permission.ACCESS_FINE_LOCATION,
//            Manifest.permission.ACCESS_BACKGROUND_LOCATION,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.MANAGE_EXTERNAL_STORAGE,
        Manifest.permission.REQUEST_INSTALL_PACKAGES,
        Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
        Manifest.permission.CALL_PHONE,
        Manifest.permission.SEND_SMS
//        Manifest.permission.READ_PHONE_STATE
    )


    companion object {
        val instance = PermissionUtil()
    }

    /**
     * @time 2024/4/11  13:39
     * @author Im_jingwei
     * @desc 请求权限
     */
    fun requesetPermission(activity: Activity?) {
        mActivity = activity
        for (eachPermission in REQUIRED_PERMISSION_LIST) {
            if (ContextCompat.checkSelfPermission(mActivity!!, eachPermission) != PackageManager.PERMISSION_GRANTED) {
                missingPermission.add(eachPermission)
            }
        }
        if (missingPermission.isNotEmpty()) {
            ActivityCompat.requestPermissions(mActivity!!, missingPermission.toTypedArray(), REQUEST_PERMISSION_CODE)
        }
    }

    fun requesetPermission(activity: Activity, permission: String) {
        if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
            val list: MutableList<String> = ArrayList()
            list.add(permission)
            ActivityCompat.requestPermissions(activity, list.toTypedArray(), REQUEST_PERMISSION_CODE)
        }
    }

    fun checkPermisssion(activity: Activity, permission: String): Boolean {
        return ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED
    }


    /**
     * @time 2024/4/11  13:43
     * @author Im_jingwei
     * @desc 权限回调
     */
    fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {
        if (requestCode == REQUEST_PERMISSION_CODE) {
            for (i in grantResults.indices.reversed()) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    missingPermission.remove(permissions[i])
                }
            }
//            checkStorageManagerPermission(mActivity!!.baseContext)
        }
    }

    /**
     *  @time 2024/10/31  14:18
     *  @author Im_jingwei
     *  @desc 请求所有文件权限
     */
    fun checkStorageManagerPermission(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
            val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.data = Uri.parse("package:" + context.getPackageName())
            context.startActivity(intent)
        }
    }
}